/**
 * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package com.vijayan.profile.service.persistence;

import com.liferay.portal.NoSuchModelException;
import com.liferay.portal.kernel.annotation.BeanReference;
import com.liferay.portal.kernel.cache.CacheRegistryUtil;
import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
import com.liferay.portal.kernel.dao.orm.FinderPath;
import com.liferay.portal.kernel.dao.orm.Query;
import com.liferay.portal.kernel.dao.orm.QueryUtil;
import com.liferay.portal.kernel.dao.orm.Session;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.util.GetterUtil;
import com.liferay.portal.kernel.util.InstanceFactory;
import com.liferay.portal.kernel.util.OrderByComparator;
import com.liferay.portal.kernel.util.StringBundler;
import com.liferay.portal.kernel.util.StringUtil;
import com.liferay.portal.model.ModelListener;
import com.liferay.portal.service.persistence.BatchSessionUtil;
import com.liferay.portal.service.persistence.ResourcePersistence;
import com.liferay.portal.service.persistence.UserPersistence;
import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;

import com.vijayan.profile.NoSuchHoroscopeException;
import com.vijayan.profile.model.Horoscope;
import com.vijayan.profile.model.impl.HoroscopeImpl;
import com.vijayan.profile.model.impl.HoroscopeModelImpl;

import java.io.Serializable;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * The persistence implementation for the horoscope service.
 *
 * <p>
 * Never modify or reference this class directly. Always use {@link HoroscopeUtil} to access the horoscope persistence. Modify <code>service.xml</code> and rerun ServiceBuilder to regenerate this class.
 * </p>
 *
 * <p>
 * Caching information and settings can be found in <code>portal.properties</code>
 * </p>
 *
 * @author vijayan
 * @see HoroscopePersistence
 * @see HoroscopeUtil
 * @generated
 */
public class HoroscopePersistenceImpl extends BasePersistenceImpl<Horoscope>
	implements HoroscopePersistence {
	public static final String FINDER_CLASS_NAME_ENTITY = HoroscopeImpl.class.getName();
	public static final String FINDER_CLASS_NAME_LIST = FINDER_CLASS_NAME_ENTITY +
		".List";
	public static final FinderPath FINDER_PATH_FIND_ALL = new FinderPath(HoroscopeModelImpl.ENTITY_CACHE_ENABLED,
			HoroscopeModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
			"findAll", new String[0]);
	public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(HoroscopeModelImpl.ENTITY_CACHE_ENABLED,
			HoroscopeModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
			"countAll", new String[0]);

	/**
	 * Caches the horoscope in the entity cache if it is enabled.
	 *
	 * @param horoscope the horoscope to cache
	 */
	public void cacheResult(Horoscope horoscope) {
		EntityCacheUtil.putResult(HoroscopeModelImpl.ENTITY_CACHE_ENABLED,
			HoroscopeImpl.class, horoscope.getPrimaryKey(), horoscope);
	}

	/**
	 * Caches the horoscopes in the entity cache if it is enabled.
	 *
	 * @param horoscopes the horoscopes to cache
	 */
	public void cacheResult(List<Horoscope> horoscopes) {
		for (Horoscope horoscope : horoscopes) {
			if (EntityCacheUtil.getResult(
						HoroscopeModelImpl.ENTITY_CACHE_ENABLED,
						HoroscopeImpl.class, horoscope.getPrimaryKey(), this) == null) {
				cacheResult(horoscope);
			}
		}
	}

	/**
	 * Clears the cache for all horoscopes.
	 *
	 * <p>
	 * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
	 * </p>
	 */
	public void clearCache() {
		CacheRegistryUtil.clear(HoroscopeImpl.class.getName());
		EntityCacheUtil.clearCache(HoroscopeImpl.class.getName());
		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
	}

	/**
	 * Clears the cache for the horoscope.
	 *
	 * <p>
	 * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
	 * </p>
	 */
	public void clearCache(Horoscope horoscope) {
		EntityCacheUtil.removeResult(HoroscopeModelImpl.ENTITY_CACHE_ENABLED,
			HoroscopeImpl.class, horoscope.getPrimaryKey());
	}

	/**
	 * Creates a new horoscope with the primary key. Does not add the horoscope to the database.
	 *
	 * @param horoscopeId the primary key for the new horoscope
	 * @return the new horoscope
	 */
	public Horoscope create(long horoscopeId) {
		Horoscope horoscope = new HoroscopeImpl();

		horoscope.setNew(true);
		horoscope.setPrimaryKey(horoscopeId);

		return horoscope;
	}

	/**
	 * Removes the horoscope with the primary key from the database. Also notifies the appropriate model listeners.
	 *
	 * @param primaryKey the primary key of the horoscope to remove
	 * @return the horoscope that was removed
	 * @throws com.liferay.portal.NoSuchModelException if a horoscope with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public Horoscope remove(Serializable primaryKey)
		throws NoSuchModelException, SystemException {
		return remove(((Long)primaryKey).longValue());
	}

	/**
	 * Removes the horoscope with the primary key from the database. Also notifies the appropriate model listeners.
	 *
	 * @param horoscopeId the primary key of the horoscope to remove
	 * @return the horoscope that was removed
	 * @throws com.vijayan.profile.NoSuchHoroscopeException if a horoscope with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public Horoscope remove(long horoscopeId)
		throws NoSuchHoroscopeException, SystemException {
		Session session = null;

		try {
			session = openSession();

			Horoscope horoscope = (Horoscope)session.get(HoroscopeImpl.class,
					new Long(horoscopeId));

			if (horoscope == null) {
				if (_log.isWarnEnabled()) {
					_log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + horoscopeId);
				}

				throw new NoSuchHoroscopeException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
					horoscopeId);
			}

			return remove(horoscope);
		}
		catch (NoSuchHoroscopeException nsee) {
			throw nsee;
		}
		catch (Exception e) {
			throw processException(e);
		}
		finally {
			closeSession(session);
		}
	}

	protected Horoscope removeImpl(Horoscope horoscope)
		throws SystemException {
		horoscope = toUnwrappedModel(horoscope);

		Session session = null;

		try {
			session = openSession();

			if (horoscope.isCachedModel() || BatchSessionUtil.isEnabled()) {
				Object staleObject = session.get(HoroscopeImpl.class,
						horoscope.getPrimaryKeyObj());

				if (staleObject != null) {
					session.evict(staleObject);
				}
			}

			session.delete(horoscope);

			session.flush();
		}
		catch (Exception e) {
			throw processException(e);
		}
		finally {
			closeSession(session);
		}

		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);

		EntityCacheUtil.removeResult(HoroscopeModelImpl.ENTITY_CACHE_ENABLED,
			HoroscopeImpl.class, horoscope.getPrimaryKey());

		return horoscope;
	}

	public Horoscope updateImpl(com.vijayan.profile.model.Horoscope horoscope,
		boolean merge) throws SystemException {
		horoscope = toUnwrappedModel(horoscope);

		Session session = null;

		try {
			session = openSession();

			BatchSessionUtil.update(session, horoscope, merge);

			horoscope.setNew(false);
		}
		catch (Exception e) {
			throw processException(e);
		}
		finally {
			closeSession(session);
		}

		FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);

		EntityCacheUtil.putResult(HoroscopeModelImpl.ENTITY_CACHE_ENABLED,
			HoroscopeImpl.class, horoscope.getPrimaryKey(), horoscope);

		return horoscope;
	}

	protected Horoscope toUnwrappedModel(Horoscope horoscope) {
		if (horoscope instanceof HoroscopeImpl) {
			return horoscope;
		}

		HoroscopeImpl horoscopeImpl = new HoroscopeImpl();

		horoscopeImpl.setNew(horoscope.isNew());
		horoscopeImpl.setPrimaryKey(horoscope.getPrimaryKey());

		horoscopeImpl.setHoroscopeId(horoscope.getHoroscopeId());
		horoscopeImpl.setProfileId(horoscope.getProfileId());
		horoscopeImpl.setSuryan(horoscope.getSuryan());
		horoscopeImpl.setChandra(horoscope.getChandra());
		horoscopeImpl.setKuja(horoscope.getKuja());
		horoscopeImpl.setBudha(horoscope.getBudha());
		horoscopeImpl.setGuru(horoscope.getGuru());
		horoscopeImpl.setSukra(horoscope.getSukra());
		horoscopeImpl.setSani(horoscope.getSani());
		horoscopeImpl.setRahu(horoscope.getRahu());
		horoscopeImpl.setKetu(horoscope.getKetu());
		horoscopeImpl.setMandhi(horoscope.getMandhi());
		horoscopeImpl.setLagna(horoscope.getLagna());

		return horoscopeImpl;
	}

	/**
	 * Finds the horoscope with the primary key or throws a {@link com.liferay.portal.NoSuchModelException} if it could not be found.
	 *
	 * @param primaryKey the primary key of the horoscope to find
	 * @return the horoscope
	 * @throws com.liferay.portal.NoSuchModelException if a horoscope with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public Horoscope findByPrimaryKey(Serializable primaryKey)
		throws NoSuchModelException, SystemException {
		return findByPrimaryKey(((Long)primaryKey).longValue());
	}

	/**
	 * Finds the horoscope with the primary key or throws a {@link com.vijayan.profile.NoSuchHoroscopeException} if it could not be found.
	 *
	 * @param horoscopeId the primary key of the horoscope to find
	 * @return the horoscope
	 * @throws com.vijayan.profile.NoSuchHoroscopeException if a horoscope with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public Horoscope findByPrimaryKey(long horoscopeId)
		throws NoSuchHoroscopeException, SystemException {
		Horoscope horoscope = fetchByPrimaryKey(horoscopeId);

		if (horoscope == null) {
			if (_log.isWarnEnabled()) {
				_log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + horoscopeId);
			}

			throw new NoSuchHoroscopeException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
				horoscopeId);
		}

		return horoscope;
	}

	/**
	 * Finds the horoscope with the primary key or returns <code>null</code> if it could not be found.
	 *
	 * @param primaryKey the primary key of the horoscope to find
	 * @return the horoscope, or <code>null</code> if a horoscope with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public Horoscope fetchByPrimaryKey(Serializable primaryKey)
		throws SystemException {
		return fetchByPrimaryKey(((Long)primaryKey).longValue());
	}

	/**
	 * Finds the horoscope with the primary key or returns <code>null</code> if it could not be found.
	 *
	 * @param horoscopeId the primary key of the horoscope to find
	 * @return the horoscope, or <code>null</code> if a horoscope with the primary key could not be found
	 * @throws SystemException if a system exception occurred
	 */
	public Horoscope fetchByPrimaryKey(long horoscopeId)
		throws SystemException {
		Horoscope horoscope = (Horoscope)EntityCacheUtil.getResult(HoroscopeModelImpl.ENTITY_CACHE_ENABLED,
				HoroscopeImpl.class, horoscopeId, this);

		if (horoscope == null) {
			Session session = null;

			try {
				session = openSession();

				horoscope = (Horoscope)session.get(HoroscopeImpl.class,
						new Long(horoscopeId));
			}
			catch (Exception e) {
				throw processException(e);
			}
			finally {
				if (horoscope != null) {
					cacheResult(horoscope);
				}

				closeSession(session);
			}
		}

		return horoscope;
	}

	/**
	 * Finds all the horoscopes.
	 *
	 * @return the horoscopes
	 * @throws SystemException if a system exception occurred
	 */
	public List<Horoscope> findAll() throws SystemException {
		return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
	}

	/**
	 * Finds a range of all the horoscopes.
	 *
	 * <p>
	 * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
	 * </p>
	 *
	 * @param start the lower bound of the range of horoscopes to return
	 * @param end the upper bound of the range of horoscopes to return (not inclusive)
	 * @return the range of horoscopes
	 * @throws SystemException if a system exception occurred
	 */
	public List<Horoscope> findAll(int start, int end)
		throws SystemException {
		return findAll(start, end, null);
	}

	/**
	 * Finds an ordered range of all the horoscopes.
	 *
	 * <p>
	 * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
	 * </p>
	 *
	 * @param start the lower bound of the range of horoscopes to return
	 * @param end the upper bound of the range of horoscopes to return (not inclusive)
	 * @param orderByComparator the comparator to order the results by
	 * @return the ordered range of horoscopes
	 * @throws SystemException if a system exception occurred
	 */
	public List<Horoscope> findAll(int start, int end,
		OrderByComparator orderByComparator) throws SystemException {
		Object[] finderArgs = new Object[] {
				String.valueOf(start), String.valueOf(end),
				String.valueOf(orderByComparator)
			};

		List<Horoscope> list = (List<Horoscope>)FinderCacheUtil.getResult(FINDER_PATH_FIND_ALL,
				finderArgs, this);

		if (list == null) {
			Session session = null;

			try {
				session = openSession();

				StringBundler query = null;
				String sql = null;

				if (orderByComparator != null) {
					query = new StringBundler(2 +
							(orderByComparator.getOrderByFields().length * 3));

					query.append(_SQL_SELECT_HOROSCOPE);

					appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
						orderByComparator);

					sql = query.toString();
				}
				else {
					sql = _SQL_SELECT_HOROSCOPE;
				}

				Query q = session.createQuery(sql);

				if (orderByComparator == null) {
					list = (List<Horoscope>)QueryUtil.list(q, getDialect(),
							start, end, false);

					Collections.sort(list);
				}
				else {
					list = (List<Horoscope>)QueryUtil.list(q, getDialect(),
							start, end);
				}
			}
			catch (Exception e) {
				throw processException(e);
			}
			finally {
				if (list == null) {
					list = new ArrayList<Horoscope>();
				}

				cacheResult(list);

				FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs, list);

				closeSession(session);
			}
		}

		return list;
	}

	/**
	 * Removes all the horoscopes from the database.
	 *
	 * @throws SystemException if a system exception occurred
	 */
	public void removeAll() throws SystemException {
		for (Horoscope horoscope : findAll()) {
			remove(horoscope);
		}
	}

	/**
	 * Counts all the horoscopes.
	 *
	 * @return the number of horoscopes
	 * @throws SystemException if a system exception occurred
	 */
	public int countAll() throws SystemException {
		Object[] finderArgs = new Object[0];

		Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
				finderArgs, this);

		if (count == null) {
			Session session = null;

			try {
				session = openSession();

				Query q = session.createQuery(_SQL_COUNT_HOROSCOPE);

				count = (Long)q.uniqueResult();
			}
			catch (Exception e) {
				throw processException(e);
			}
			finally {
				if (count == null) {
					count = Long.valueOf(0);
				}

				FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
					count);

				closeSession(session);
			}
		}

		return count.intValue();
	}

	/**
	 * Initializes the horoscope persistence.
	 */
	public void afterPropertiesSet() {
		String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
					com.liferay.util.service.ServiceProps.get(
						"value.object.listener.com.vijayan.profile.model.Horoscope")));

		if (listenerClassNames.length > 0) {
			try {
				List<ModelListener<Horoscope>> listenersList = new ArrayList<ModelListener<Horoscope>>();

				for (String listenerClassName : listenerClassNames) {
					listenersList.add((ModelListener<Horoscope>)InstanceFactory.newInstance(
							listenerClassName));
				}

				listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
			}
			catch (Exception e) {
				_log.error(e);
			}
		}
	}

	@BeanReference(type = ProfilePersistence.class)
	protected ProfilePersistence profilePersistence;
	@BeanReference(type = MyExpectationPersistence.class)
	protected MyExpectationPersistence myExpectationPersistence;
	@BeanReference(type = FamilyMemberPersistence.class)
	protected FamilyMemberPersistence familyMemberPersistence;
	@BeanReference(type = HoroscopePersistence.class)
	protected HoroscopePersistence horoscopePersistence;
	@BeanReference(type = MyPhotoPersistence.class)
	protected MyPhotoPersistence myPhotoPersistence;
	@BeanReference(type = ResourcePersistence.class)
	protected ResourcePersistence resourcePersistence;
	@BeanReference(type = UserPersistence.class)
	protected UserPersistence userPersistence;
	private static final String _SQL_SELECT_HOROSCOPE = "SELECT horoscope FROM Horoscope horoscope";
	private static final String _SQL_COUNT_HOROSCOPE = "SELECT COUNT(horoscope) FROM Horoscope horoscope";
	private static final String _ORDER_BY_ENTITY_ALIAS = "horoscope.";
	private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No Horoscope exists with the primary key ";
	private static Log _log = LogFactoryUtil.getLog(HoroscopePersistenceImpl.class);
}